package com.ceba.test.modules.filemanage.helper;

import java.io.File;

import com.ceba.base.helper.LoggerHelper;
import com.ceba.base.utils.ToPdfUtils;
import com.ceba.cebautils.StringUtil;
import com.ceba.test.base.config.LogicPathUtil;
import com.ceba.test.modules.storage.logicService.StorageFileCacheManage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.base.configs.CebaPathUtil;
import com.ceba.test.base.language.ResourceUtils;
import com.ceba.test.base.utils.CebaFileManageUtil;
import com.ceba.test.modules.filemanage.entity.GFile;
import com.ceba.test.modules.filemanage.logicService.FileManageLogicService;

import abc.ID;
import abc.util.FileHelper;
import configs.context.SpringBeans;

/**
 * 文件查看
 * 
 *
 */
@SuppressWarnings("deprecation")
public class FileViewObject {
	static Logger logger = LoggerFactory.getLogger(FileViewObject.class);
	private String extName = "";
	private String fileId = "";
	private String context = ResourceUtils.getMessage("MODULES_DISPLAY_CONTENT_PLEASE_DOWNLOAD_FILE");
	private String url = "";

	public FileViewObject(String Id,String userId) throws Exception {
		this.fileId = Id;
		view(userId);
	}

	public String getExtName() {
		return this.extName;
	}

	public String getContext() {
		return this.context;
	}

	public String getUrl() {
		return this.url;
	}

	/**
	 * 文件查看，包括word转pdf等一系列动作，方法可以直接用网页查看
	 * 
	 * @return
	 * @throws Exception
	 */
	public String view(String userId) {
		try {
			//获取当前正在使用的文件存储Id
		  String storageId =  StorageFileCacheManage.getSystemUseAddressId();
			FileManageLogicService  fileManageLogicService =
					SpringBeans.getFileManageLogicService("fileManageLogicService");
			GFile gFile = fileManageLogicService.getFilePath(fileId);
			if (gFile != null) {
				String filePath = gFile.getFilePath();
				filePath = LogicPathUtil.getAbsolutelyPath(gFile.getStorageAddressId(),gFile.getVirtualPath());
				if (!StringUtil.isEmptyString(filePath)) {
					File file = new File(filePath);
					if (file.isFile()) {
						extName = FileHelper.getFileExtName(filePath).toLowerCase();
						String outPath = LogicPathUtil.getPhysicalTempDirPath(storageId)+ ID.getGuid() + ".pdf";
						if (ToPdfUtils.boolChangePdfByExtName(filePath)) {
							if (ToPdfUtils.officeToPdf(filePath, outPath)) {
								url =LogicPathUtil.getBrowserPath(storageId,LogicPathUtil.getVirtualPath(storageId,outPath));
							}
						} else if (extName.equals("txt")) {
							// txt文件直接读取
							context = FileHelper.getFileContent(file, ToPdfUtils.getCharset(filePath));
							url =LogicPathUtil.getBrowserPath(storageId,LogicPathUtil.getVirtualPath(storageId,outPath));
						} else {
							url = LogicPathUtil.getBrowserPath(gFile.getStorageAddressId(),gFile.getVirtualPath());
						}
					}
				}
			}
		} catch (Exception e) {
			LoggerHelper.error(logger, e, "");
		}
		return url;
	}
	
	
	public static void main(String []args){
		try {
	//		ZipUtils.unzip(
	//			"E:/setup/sql.zip",
	//			"E:/setup/111","201606ABC");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}